package br.com.projeto.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.StrutsStatics;

import br.com.projeto.entity.UsuarioVO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class InterceptadorLogin extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation actioninvocation) throws Exception {
		final ActionContext context = actioninvocation.getInvocationContext();
	    HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
	    HttpSession session = request.getSession(true);
	    //Tenta recuperar o atributo usuario da sessao
	    UsuarioVO user = (UsuarioVO) session.getAttribute("usuarioVO");
	    
	    if (user == null) {
	    	
	    	return actioninvocation.invoke();
	    } else if(actioninvocation.invoke().equals("cadastro")){
	    		return "success";
	    	}else{
	    		return actioninvocation.invoke();
	    	}
    }
}
	


